<?

class bf_html {

	private $opts;

	public function __construct($options = array()) {
		$this->opts = array_merge(array(), $options);
	}
	
	public function input($options = array()) {

		$opts = array_merge(array(
			'type' => 'text',
			'name' => '',
			'value' => '',
			'id' => '',
			'class' => '',
			'title' => '',
			'checked' => false,
			'autocomplete' => true,
			'disabled' => false,
			'label-before' => '',
			'label-after' => '',
		), $options);

		return ''
			. ($opts['label-before'] ? "<label" . ($opts['id'] ? " for=\"{$opts['id']}\"" : '') . ">" . $this->escape($opts['label-before']) . "</label>" : '')
			. '<input type="' . $opts['type'] . '"'
			. ($opts['id'] ? " id=\"{$opts['id']}\"" : '')
			. ($opts['name'] ? " name=\"{$opts['name']}\"" : '')
			. ($opts['class'] ? " class=\"{$opts['class']}\"" : '')
			. ($opts['disabled'] ? " disabled=\"disabled\"" : '')
			. ($opts['checked'] ? " checked=\"checked\"" : '')
			. (!$opts['autocomplete'] ? " autocomplete=\"off\"" : '')
			. ($opts['value'] !== '' ? " value=\"" . $this->escape($opts['value']) . "\"" : '')
			. ($opts['title'] ? " title=\"" . $this->escape($opts['title']) . "\"" : '')
			. "/>"
			. ($opts['label-after'] ? "<label" . ($opts['id'] ? " for=\"{$opts['id']}\"" : '') . ">" . $this->escape($opts['label-after']) . "</label>" : '')
		;
	}

	public function field($options = array()) {
		if (!@$options['class']) $options['class'] = 'field';
		return $this->input($options);
	}

	public function checkbox($options = array()) {
		$options['type'] = 'checkbox';
		return $this->input($options);
	}

	public function radio($options = array()) {
		$options['type'] = 'checkbox';
		return $this->input($options);
	}

	public function hidden($options = array()) {
		$options['type'] = 'hidden';
		return $this->input($options);
	}

	public function select($options = array()) {

		$opts = array_merge(array(
			'lang' => 'ru',
			'selected' => '',
			'data' => array(),
			'column-id' => 'id',
			'column-name' => 'name',
			'name' => '',
			'id' => '',
			'class' => '',
			'title' => '',
			'disabled' => false,
			'all-value' => '',
			'all-label' => '-- All --',
			'all-style' => '',
			'all-option' => false
		), $options);
		
		$html = '<select'
			. ($opts['id'] ? " id=\"{$opts['id']}\"" : '')
			. ($opts['name'] ? " name=\"{$opts['name']}\"" : '')
			. ($opts['class'] ? " class=\"{$opts['class']}\"" : '')
			. ($opts['disabled'] && !is_array($opts['disabled']) ? " disabled=\"disabled\"" : '')
			. ($opts['title'] ? " title=\"" . $this->escape($opts['title']) . "\"" : '')
			. ">\n"
		;

		if ($opts['all-option']) {
			$html .= '<option value="' . $opts['all-value'] . '"'
					. ($opts['all-value'] == $opts['selected'] ? ' selected="selected"' : '')
					. ($opts['all-style'] ? ' style="' . $opts['all-style'] . '"' : '')
					. (is_array($opts['disabled']) && in_array($opts['all-value'], $opts['disabled']) ? ' disabled="disabled"' : '')
					. '>' . $opts['all-label'] . "</option>\n"
				;
		}

		$id = $opts['column-id'];
		$name = $opts['column-name'];
		foreach ($opts['data'] as $row)
			if (is_array($row)) {
				$html .= '<option value="' . $row[$id] . '"'
					. ($row[$id] == $opts['selected'] ? ' selected="selected"' : '')
					. (@$row['label'] ? ' label="' . $this>escape($row['label']) . '"' : '')
					. (@$row['style'] ? ' style="' . $row['style'] . '"' : '')
					. (is_array($opts['disabled']) && in_array($row[$id], $opts['disabled']) ? ' disabled="disabled"' : '')
					. '>' . $this->escape($row[$name]) . "</option>\n"
				;
			} else if (is_object($row)) {
				$html .= '<option value="' . $row->$id . '"'
					. ($row->$id == $opts['selected'] ? ' selected="selected"' : '')
					. (@$row->label ? ' label="' . $this>escape($row->label) . '"' : '')
					. (@$row->style ? ' style="' . $row->style . '"' : '')
					. (is_array($opts['disabled']) && in_array($row->$id, $opts['disabled']) ? ' disabled="disabled"' : '')
					. '>' . $this->escape($row->$name) . "</option>\n"
				;
			}
		return $html . "</select>\n";
	}
	
	public function script($options = array()) {

		$opts = array_merge(array(
			'data' => '',
			'src' => '',
			'type' => 'text/javascript',
			'charset' => 'utf-8',
		), $options);

		return "<script type=\"{$opts['type']}\" charset=\"{$opts['charset']}\"".($opts['src'] ? " src=\"{$opts['src']}\"" : '').">{$opts['data']}</script>\n";
	}

	public function css($options = array()) {

		$opts = array_merge(array(
			'data' => '',
			'href' => '',
			'rel' => 'stylesheet',
			'type' => 'text/css',
		), $options);

		return ($opts['data'] ? "<style>\n{$opts['data']}\n</style>\n"
			: "<link href=\"{$opts['href']}\" rel=\"{$opts['rel']}\" type=\"{$opts['type']}\"/>"); 
	}
	
	public function pages($options = array()) {

		$opts = array_merge(array(
			'first' => 1,
			'selected' => 1,
			'last' => 100,
			'visual-shift' => 0,
			'range' => 3,
			'label' => '',
			'base-url' => '',
			'all-label' => 'All',
			'all-url' => null,
			'id' => '',
			'class' => '',
		), $options);
			
		$min = $opts['selected'] - $opts['range'];
		$max = $opts['selected'] + $opts['range'];
		
		if ($min < $opts['first']) {
			$max += -($min - $opts['first']);
			$min = $opts['first'];
		}
		
		if ($max > $opts['last']) {
			$min -= ($max - $opts['last']);
			$max = $opts['last'];
			if ($min < $opts['first'])
				$min = $opts['first'];
		}

		$content = '';
		//$content = '<div class="pages"><label>' . $opts['label'] . '</label>';
		if ($min > $opts['first'] + 1) {
			$content .= '<a href="' . $opts['base-url'] . $opts['first'] . '"' . ($opts['first'] == $opts['selected'] ? ' class="selected"' : '') . '>' . ($opts['first'] + $opts['visual-shift']) . '</a>';
			$content .= '<a href="' . $opts['base-url'] . ($min - 1) . '"' . (($min - 1) == $opts['selected'] ? ' class="selected"' : '') . '>...</a>';
		} else if ($min > $opts['first']) {
			$content .= '<a href="' . $opts['base-url'] . $opts['first'] . '"' . ($opts['first'] == $opts['selected'] ? ' class="selected"' : '') . '>' . ($opts['first'] + $opts['visual-shift']) . '</a>';
		}
		for ($i = $min; $i <= $max; $i ++)
			$content .= '<a href="' . $opts['base-url'] . $i . '"' . ($i == $opts['selected'] ? ' class="selected"' : '') . '>' . ($i + $opts['visual-shift']) . '</a>';
		if ($max + 1 < $opts['last']) {
			$content .= '<a href="' . $opts['base-url'] . ($max + 1) . '"' . (($max + 1) == $opts['selected'] ? ' class="selected"' : '') . '>...</a>';
			$content .= '<a href="' . $opts['base-url'] . $opts['last'] . '"' . ($opts['last'] == $opts['selected'] ? ' class="selected"' : '') . '>' . ($opts['last'] + $opts['visual-shift']) . '</a>';
		} else if ($max < $opts['last']) {
			$content .= '<a href="' . $opts['base-url'] . $opts['last'] . '"' . ($opts['last'] == $opts['selected'] ? ' class="selected"' : '') . '>' . ($opts['last'] + $opts['visual-shift']) . '</a>';
		}
		if ($opts['all-url'] !== null) {
			$content .= '<a href="' . $opts['all-url']. '" class="all-pages">' . $opts['all-label'] . '</a>';
		}

		if ($content !== '') {
			$content = '<div class="pages"><label>' . $opts['label'] . '</label>' . $content . '</div>';
		}
		
		return $content;
	}

	public function escape($value) {
		return htmlspecialchars($value);
	}

	public function unescape($value) {
		return htmlspecialchars_decode($value);
	}
}

?>